package com.joshondesign.sdljava.demo;

import com.joshondesign.sdljava.*;
import com.joshondesign.sdljava.gfx.Util;

/**
 * Created by IntelliJ IDEA.
 * User: josh
 * Date: 9/2/11
 * Time: 1:00 PM
 * To change this template use File | Settings | File Templates.
 */
public class Demo2 implements SDLUtil.InitCallback{
    private boolean pressed;

    public static void main(String ... args) {
        SDLUtil.init(new Demo2());
    }

    public void callback() {
        try {
            Util.standard_init();
            Util.dump(SDL.SDL_GetVideoInfo());
            SDL_Surface surface = Util.createResizableWindow(800, 600);
            clearScreen(surface);
            SDL_Event event = new SDL_Event();
            int px = 0;
            int py = 0;
            while(true) {
                SDL.SDL_PollEvent(event);
                if(event.getType() == SDL_EventType.SDL_QUIT) { break; }
                if(event.getType() == SDL_EventType.SDL_KEYDOWN) {
                    String key = SDL.SDL_GetKeyName(event.getKey().getKeysym().getSym());
                    if("q".equals(key)) break;
                }


                if(event.getType() == SDL_EventType.SDL_MOUSEMOTION) {
                    if(pressed) {
                        int x = event.getMotion().getX();
                        int y = event.getMotion().getY();
                        //p("dragged " + x + " " + y);
                        //clearScreen(surface);
                        long color = 0xFFFFFF;
                        fillRect(surface,px,py, 380,380, 0xFFFFFF);
                        fillRect(surface,x,y,   380,380, 0x00FFFF);
                        px = x;
                        py = y;
                        SDL.SDL_UpdateRect(surface, px, py, 20,20);
                    }
                }
                if(event.getType() == SDL_EventType.SDL_MOUSEBUTTONDOWN) {
                    pressed = true;
                }
                if(event.getType() == SDL_EventType.SDL_MOUSEBUTTONUP) {
                    pressed = false;
                }
            }

            SDL.SDL_Quit();
            p("quit SDL");
            } catch (SDLException ex) {
                ex.printStackTrace();
                SDL.SDL_Quit();
                System.exit(1);
            }

    }

    public static void fillRect(SDL_Surface surface, int x, int y, int w, int h, int rgb) {
        SDL_Rect rect = new SDL_Rect();
        rect.setX((short) x);
        rect.setY((short) y);
        rect.setW(w);
        rect.setH(h);
        long color = SDL.SDL_MapRGB(surface.getFormat()
                , (short)((rgb&0xFF0000)>>16)
                , (short)((rgb&0x00FF00)>>8)
                , (short)((rgb&0x0000FF)>>0)
                );
        SDL.SDL_FillRect(surface, rect, color);
    }

    public static void clearScreen(SDL_Surface surface) {
        SDL_Rect rect = new SDL_Rect();
        rect.setX((short) 0);
        rect.setY((short) 0);
        rect.setW(surface.getW());
        rect.setH(surface.getH());
        long white = SDL.SDL_MapRGB(surface.getFormat(), (short)0xff,(short)0xff,(short)0xff);
        SDL.SDL_FillRect(surface, rect, white);
    }

    private void p(String s) {
        System.out.println(s);
    }
}
